﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using EmbeddedSensorCloud.Core.Server;
using System.Net.Sockets;
using EmbeddedSensorCloud.HTTP;
using System.Net;

namespace EmbeddedSensorCloud.UnitTest
{
    [TestClass]
    public class ServerCore_Test
    {
        private void ThrowErrorMethod(NetworkStream stream, Request req)
        {
            throw new Exception();
        }

        [TestMethod]
        [Description("Tests if server starts with valid arguments")]
        public void ServerCore_Start_Valid_Arguments_Test()
        {
            ServerCore core = new ServerCore(ThrowErrorMethod);
            core.Start(IPAddress.Loopback, 8080);
            Assert.IsTrue(core.Listening);
        }

        [TestMethod]
        [Description("Tests if server starts with a null ip")]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ServerCore_Start_Null_IP_Test()
        {
            ServerCore core = new ServerCore(ThrowErrorMethod);
            core.Start(null, 8078);
        }

        [TestMethod]
        [Description("Tests if server starts with a none ip")]
        [ExpectedException(typeof(InvalidIPException))]
        public void ServerCore_Start_None_IP_Test()
        {
            ServerCore core = new ServerCore(ThrowErrorMethod);
            core.Start(IPAddress.None, 8079);
        }

        [TestMethod]
        [Description("Tests if server starts with a negative port")]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void ServerCore_Start_Negative_Port_Test()
        {
            ServerCore core = new ServerCore(ThrowErrorMethod);
            core.Start(IPAddress.Loopback, -48);
        }

        [TestMethod]
        [Description("Tests if server starts if server is already listening")]
        [ExpectedException(typeof(ServerAlreadyListeningException))]
        public void ServerCore_Start_Already_Listening_Test()
        {
            ServerCore core = new ServerCore(ThrowErrorMethod);
            core.Start(IPAddress.Loopback, 8081);
            core.Start(IPAddress.Loopback, 8082);
            core.Stop();
        }

        [TestMethod]
        [Description("Tests if server stops when it is not listening")]
        [ExpectedException(typeof(ServerNotListeningException))]
        public void ServerCore_Stop_Not_Listening_Test()
        {
            ServerCore core = new ServerCore(ThrowErrorMethod);
            core.Stop();
        }


    }
}
